package com.yunxi.controller.sys;

import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import com.yunxi.core.util.DESUtil;
import com.yunxi.core.web.ApiResult;
import com.yunxi.core.web.CurrentUser;
import com.yunxi.core.web.LoginInfo;
import com.yunxi.core.web.ResponseUtil;
import com.yunxi.sys.pojo.UserVO;
import com.yunxi.sys.service.UserDubboService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * Created by weipan 2018/10/31.
 */
@RestController
@RequestMapping("/admin/user")
@Api(tags = "sys:users", description = "用户管理接口")
public class UserController {
    private static final Logger logger = LoggerFactory.getLogger(UserController.class);

    @Autowired
    UserDubboService userDubboService;

    /**
     * 重置用户密码
     * @return
     */
    @PostMapping("/resetPassword")
    @ApiOperation(value = "重置用户密码")
    public ApiResult resetPassword(@RequestBody UserVO user) throws Exception {
        userDubboService.resetPassword(user);
        return ResponseUtil.ok("success");
    }

    /**
     * 导出excel
     * @return
     */
    @GetMapping("exportExcel")
    @ApiOperation(value = "导出excel")
    public ApiResult exportExcel(@RequestParam Map<String, Object> params) throws Exception {
        params.put("pageSize", null);
        List userList = userDubboService.exportExcel(params);
        return ResponseUtil.ok("用户列表", userList);
    }

    /**
     * 用户管理
     *
     * @return
     */
    @GetMapping("/page")
    @ApiOperation(value = "用户分页查询")
    public ApiResult<PageInfo> page(@RequestParam Map<String, Object> params) throws Exception {
        logger.debug("查询用户");
        PageInfo list = userDubboService.page(params);
        return ResponseUtil.ok(list);
    }

    /**
     * 修改页面
     * @return
     */
    @GetMapping("/load")
    @ApiOperation(value = "修改页面")
    public ApiResult<UserVO> load(@RequestParam Map<String, Object> params) throws Exception {
        logger.debug("修改页面");
        UserVO tmUser = userDubboService.getCasId((String) params.get("id"));
        return ResponseUtil.ok(tmUser);
    }

    /**
     * 保存用户信息
     * @return
     */
    @PostMapping("/save")
    @ApiOperation(value = "保存用户信息")
    public ApiResult save(@RequestBody UserVO tmUser, @CurrentUser LoginInfo sysUser) throws Exception {
        userDubboService.saveCas(tmUser, sysUser);
        return ResponseUtil.ok("success");
    }

    /**
     * 修改密码
     * @return
     */
    @PostMapping("/updatePassword")
    @ApiOperation(value = "修改密码")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户id", required = true, paramType = "query", dataType = "string"),
            @ApiImplicitParam(name = "oldPassword", value = "旧密码", required = true, paramType = "query", dataType = "UserVO"),
            @ApiImplicitParam(name = "password", value = "新密码", required = true, paramType = "query", dataType = "UserVO")
    })
    public ApiResult updatePassword(@RequestBody UserVO user) throws Exception {

        UserVO oldUser = userDubboService.getCasId(user.getUserId());

        if(user.getOldPassword().equals(DESUtil.decrypt(oldUser.getPassword()))){
            userDubboService.updatePassword(user);
            return ResponseUtil.ok("success");
        }else{
            return ResponseUtil.fail(402,"旧密码输入不正确");
        }
    }

    /**
     * 修改密码
     * @return
     */
    @PostMapping("/del")
    @ApiOperation(value = "删除")
    public ApiResult del(@RequestBody List<UserVO> userList) throws Exception {
        userDubboService.removeByList(userList);
        return ResponseUtil.ok("success");
    }
}
